package tools

import (
	"crypto/md5"
	"encoding/hex"
	"fmt"
	"github.com/json-iterator/go"
	"strings"
)

// 多字段编码
func KeyEncode(sep string, v ...string) string {
	res := ""
	for i := 0; i < len(v); i++ {
		if i != 0 {
			res += sep
		}
		res += v[i]
	}
	return res
}

// 多字段解码
func KeyDecode(sep, s string) []string {
	return strings.Split(s, sep)
}

// 生成字符串MD5
func StringMD5(s string) string {
	h := md5.New()
	h.Write([]byte(s))
	return hex.EncodeToString(h.Sum(nil))
}

func ToJson(v interface{}) string {
	js, err := jsoniter.MarshalIndent(v, "", "   ")
	if err != nil {
		return fmt.Sprintf("%v\r\n", v)
	}
	return string(js)
}

func IntContains(v int, list []int) bool {
	for _, i := range list {
		if i == v {
			return true
		}
	}
	return false
}
